﻿// create object
Proggy.SiteMap = function () {

    var siteMapId = undefined;
    var tree = undefined;
    var menusArea = undefined;
    var includeMenu = undefined;
    var useLive = undefined;
    var contentArea = undefined;
    var menuBound = 0;
    var callback1Bound = false;
    var itemClickCallback = undefined;
    var openCloseCallback = undefined;
    var menuItems = undefined;

    // gets menu from link
    var getMenuFromItem = function (item) {

        // get item id
        var itemId = item.data("nodeid");

        //
        return menusArea.find("span.tree-item-menu[data-nodeid='" + itemId + "']");

    }

    // bind clicks to callback
    var bindClicksToCallback = function () {

        if (!callback1Bound) {

            // dont repeat
            callback1Bound = true;

            // check
            if (typeof (itemClickCallback) == "function") {

                // bind
                tree.on("click", "a.proggy-sm-preview-link", function (e) {

                    // 
                    e.preventDefault();

                    // get item
                    var theItem = jQuery(this);

                    // bind to callback
                    itemClickCallback(theItem);

                });

            };
        }

    };

    // bind content menu
    var bindClicksToContentMenu = function () {

        // kill menu
        menusArea.html("").fadeTo(0, 0, function () {

            // hide items
            menuItems.hide();

            // append
            menusArea.append(menuItems);

        });

        // check bound
        if (menuBound == 0) {

            // bind
            tree.on("click", "a.proggy-sm-preview-link", function (e) {

                // 
                e.preventDefault();

                // get this
                var item = jQuery(this);

                // fade Menus area
                menusArea.fadeTo(200, 0, function () {

                    // hide all menus
                    menuItems.hide();

                    // find all selected
                    var selecteds = tree.find("a.tree-item-selected");

                    // remove class
                    selecteds.removeClass("tree-item-selected");

                    // add class
                    item.addClass("tree-item-selected");

                    // find menu
                    var menu = getMenuFromItem(item);

                    // show menu
                    menu.show();

                    // show menu
                    menusArea.fadeTo(200, 1);
                });
            });

            // finally
            menuBound++;
        }

    };

    // set item clicks
    var setItemClicks = function () {

        if (!useLive) {

            // check
            if (includeMenu) {
                bindClicksToContentMenu();
            }
            else {
                bindClicksToCallback();
            }
        };

    };

    // set variables
    var setOtherVariables = function () {

        //
        contentArea = jQuery("div#" + siteMapId);
        menusArea = contentArea.find("div.treeMenus:first");
        tree = contentArea.find("ul.contentTreeUl:first");
        menuItems = tree.find("span.tree-item-menu");

    };

    // check for selected
    var checkForSelected = function () {

        // TODO: ?? use this??

        // check for selected
        //var selectedLis = tree.find("li.proggy-sm-selectedTrue");

        // select
        //selectedLis.children("a.proggy-sm-preview-link").addClass("tree-item-selected");
    };

    // bind openClose
    var bindOpenClose = function () {

        // get clicks
        //var clicks = tree.find("a.proggy-sm-link");

        // bind
        tree.on("click", "a.proggy-sm-link", function (e) {

            //
            e.preventDefault()

            // get
            var link = jQuery(this);

            // show loader
            link.addClass("proggy-sm-link-loading");

            // check
            if (menusArea.size() == 0) {
                menusArea = jQuery("<div />");
            }

            // fade menus
            menusArea.fadeTo(100, 0, function () {

                // get href
                var href = link.attr("href");

                // get
                jQuery.get(href, function (r, s, x) {

                    // create as object
                    var responseObj = jQuery(r);

                    // find sitemap
                    var siteMap = responseObj.find("ul.contentTreeUl:first");

                    // replace
                    tree.html(siteMap.html());

                    // bind variables
                    setOtherVariables();

                    // kill loader
                    link.removeClass("proggy-sm-link-loading");

                    // set item clicks
                    setItemClicks();

                    // check for selected
                    checkForSelected();

                    // check for post open/close callback
                    if (typeof (openCloseCallback) == "function") {

                        openCloseCallback();

                    };

                });

            });
        });

    };

    // set init
    this.Init = function (siteMapContainerId, includeContentMenu, useLiveLinksEtc, clickCallback, openOrCloseCallback) {

        //
        itemClickCallback = clickCallback;
        openCloseCallback = openOrCloseCallback;
        useLive = useLiveLinksEtc;
        includeMenu = includeContentMenu;
        siteMapId = siteMapContainerId;
        setOtherVariables();
        bindOpenClose();
        setItemClicks();
        checkForSelected();

    };

};